package com.ppt.javappt.pojo;

import com.ppt.javappt.component.TextBox;
import javafx.scene.Node;
import javafx.scene.control.TextArea;
import javafx.scene.shape.Rectangle;

/**
 * @author cys
 */
public class NodeFactory {
    // 从数据创建 JavaFX Node
    public static Node createNode(NodeData nodeData) {
        switch (nodeData.getType()) {
            case "Rect":
                return createRectNode((RectData)nodeData);
            case "Text":
                return createTextNode((TextBoxData)nodeData);
            default:
                throw new IllegalArgumentException("Unknown component type: " + nodeData.getType());
        }
    }
    // 从JavaFX Node 中创建数据
    public static NodeData createDataFromNode(Node node) {
        if (node instanceof Rectangle) {
            return extractRectData((Rectangle) node);
        }else if(node instanceof TextBox){
            return extractTextData((TextBox) node);
        }
        return null;
    }
    /**
     *  创建矩形对应的Pojo
     * */
    private static RectData extractRectData(Rectangle rect) {
        RectData data = new RectData();
        data.setX(rect.getX());
        data.setY(rect.getY());
        data.setHeight(rect.getHeight());
        data.setWidth(rect.getWidth());
        return data;
    }
    /**
     *  创建矩形
     * */
    private static Rectangle createRectNode(RectData data) {
        Rectangle rect = new Rectangle();
        rect.setY(data.getY());
        rect.setX(data.getX());
        rect.setHeight(data.getHeight());
        rect.setWidth(data.getWidth());
        return rect;
    }
    /**
     *  创建文本框对应的Pojo
     * */
    private static TextBoxData extractTextData(TextBox text) {
        TextBoxData data = new TextBoxData();
        data.setX(text.getLayoutX());
        data.setY(text.getLayoutY());
        data.setHeight(text.getPrefHeight());
        data.setWidth(text.getPrefWidth());
        data.setBold(text.isBold());
        data.setItalic(text.isItalic());
        data.setFontFamily(text.getFontFamily());
        data.setFontSize(text.getFontSize());
        data.setText(text.getText());
        data.setStyle(text.getStyle());
        return data;
    }
    /**
     *  创建文本框
     * */
    private static TextBox createTextNode(TextBoxData data) {
        TextBox text = new TextBox();
        text.setPosition(data.getX(),data.getY());
        text.setPrefHeight(data.getHeight());
        text.setPrefWidth(data.getWidth());
//        text.setSize(data.getHeight(),data.getWidth());
        text.setBold(data.isBold());
        text.setItalic(data.isItalic());
        text.setFontFamily(data.getFontFamily());
        text.setFontSize(data.getFontSize());
        text.setText(data.getText());
        text.setStyle(data.getStyle());
        return text;
    }
}
